package com.htc.dp.filter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: htc
 * @date: Created in 17:30 2018/6/7.
 */
public class FilterChain implements Filter {

    List<Filter> filters = new ArrayList<Filter>();
    int index = 0;

    /**
     * 链条式编程
     * @param f
     * @return
     */
    public FilterChain addFilter(Filter f) {
        this.filters.add(f);
        return this;
    }

    @Override
    public void doFilter(Request request, Response response, FilterChain chain) {
        if (index == filters.size()) {
            return;
        }
        Filter filter = filters.get(index);
        index++;
        filter.doFilter(request, response, chain);
    }
}
